<template>
  <a-card :bordered="false">
    <a-spin :spinning="ecUserChangeLoading">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="组织名称">
              <a-input placeholder="请输入组织名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="管理用户">
              <a-input placeholder="请输入用户名称" v-model="queryParam.users"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="来源">
              <j-dict-select-tag placeholder="请选择来源" v-model="queryParam.source" dictCode="settlement_org_source"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="启用状态">
              <j-search-select-tag placeholder="请选择启用状态" v-model="queryParam.status" dict="yn"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="易仓账号状态">
                <j-dict-select-tag placeholder="请选择易仓账号状态" v-model="queryParam.ecUserStatus" dictCode="finance_logistics_ec_user_status"/>
              </a-form-item>
            </a-col>
             <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="易仓发货状态">
                <j-dict-select-tag placeholder="请选择易仓发货状态" v-model="queryParam.ecStatus" dictCode="finance_logistics_ec_user_status"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="认证情况">
                <j-dict-select-tag placeholder="请选择认证情况" v-model="queryParam.authStatus" dictCode="finance_logistics_ec_auth_status"/>
              </a-form-item>
            </a-col>  
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'BaseSettlementOrg:addSync'" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('结算组织')">导出</a-button>
      <a-button v-has="'BaseSettlementOrg:addSync'" type="primary" icon="sync" :loading="syncLoading" @click="handleSyncData">同步结算组织数据</a-button>
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
<!--        <a-menu slot="overlay">-->
<!--          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
<!--        </a-menu>-->
<!--        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
<!--      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :isorter="isorter"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="selectRowData">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" :preview="0"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
         <span slot="ecStatusTitle">
           仓库发货状态
           <span>
              <a-tooltip :getPopupContainer="getPopupContainer">
                <template slot="title">
                  <div>停用规则：</div>
                  <div>账户余额&lt;7天平均额度 &nbsp;短信提醒</div> 
                  <div>账户余额≤0 短信提醒并停止发货 如24小时后账户余额≤0则停用易仓账号</div> 
                  <div>恢复规则：</div>   
                  <div>充值或给予临时额度后，账户余额大于0 恢复发货或恢复易仓账号</div>
                </template>
                <a-icon type="exclamation-circle" />
              </a-tooltip>
           </span>
         </span>
        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" /> -->
          <a v-if="record.ecStatus === 0"  v-has="'BaseSettlementOrg:ecStatus'"  @click="enableBt(record)">发货启用</a>
          <a v-if="record.ecStatus === 1" v-has="'BaseSettlementOrg:ecStatus'"  @click="disableBt(record)">发货停用</a>
          <a-divider type="vertical" v-has="'BaseSettlementOrg:ecStatus'" />
          <a v-if="record.ecUserStatus === 0" v-has="'BaseSettlementOrg:ecUserStatus'" @click="enableUserBt(record)">账号启用</a>
          <a v-if="record.ecUserStatus === 1" v-has="'BaseSettlementOrg:ecUserStatus'" @click="disableUserBt(record)">账号停用</a>
          <a-divider type="vertical" v-has="'BaseSettlementOrg:ecUserStatus'" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
                <!-- 暂时隐藏入口   0826 -->
              <a-menu-item v-if="record.authStatus=='1'">
                <a @click="handleCheck(record)">审核</a>
              </a-menu-item> 
              <a-menu-item v-has="'BaseSettlementOrg:lineConfig'">
                <a @click="handleLineConfig(record)">额度配置</a>
              </a-menu-item>
              <a-menu-item v-has="'BaseSettlementOrg:edit'">
                <a @click="handleEdit(record)">编辑</a>
              </a-menu-item>
               <!-- 暂时隐藏入口   0826 -->
            <a-menu-item>
                <a @click="handleDetails(record)">详情</a>
              </a-menu-item>  
            </a-menu>
          </a-dropdown>
<!--          <a-divider type="vertical" />-->
<!--          <a-dropdown>-->
<!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
<!--            <a-menu slot="overlay">-->
<!--&lt;!&ndash;              <a-menu-item>&ndash;&gt;-->
<!--&lt;!&ndash;                <a @click="handleDetail(record)">详情</a>&ndash;&gt;-->
<!--&lt;!&ndash;              </a-menu-item>&ndash;&gt;-->
<!--              <a-menu-item>-->
<!--&lt;!&ndash;                <a v-if="record.ecUserStatus === 0" @click="enableUserBt(record)">账号启用</a>&ndash;&gt;-->
<!--&lt;!&ndash;          <a v-if="record.ecUserStatus === 1" @click="disableUserBt(record)">账号停用</a>&ndash;&gt;-->
<!--&lt;!&ndash;                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">&ndash;&gt;-->
<!--&lt;!&ndash;                  <a>删除</a>&ndash;&gt;-->
<!--&lt;!&ndash;                </a-popconfirm>&ndash;&gt;-->
<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <base-settlement-org-modal ref="modalForm" @ok="modalFormOk"></base-settlement-org-modal>
    <base-line-config-modal ref="lineConfigModal" @ok="modalFormOk"></base-line-config-modal>
    <base-audit-modal ref="baseAuditModal" @ok="modalFormOk"></base-audit-modal>
    <!-- <base-settlemen-detail-modal ref="detailModal" @ok="modalFormOk"></base-settlemen-detail-modal> -->
    <base-settlemen-detail-modal ref="detailModal" @ok="modalFormOk"></base-settlemen-detail-modal>
</a-spin>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BaseSettlementOrgModal from './modules/BaseSettlementOrgModal'  //编辑Modal
  import BaseLineConfigModal from './modules/BaseLineConfigModal'  //额度配置Modal
  import BaseAuditModal from './modules/BaseAuditModal.vue'   //审核Modal
  import BaseSettlemenDetailModal from './modules/BaseSettlemenDetailModal.vue'//详情Modal
  import {postFormAction,getAction,postAction} from "@api/manage";



  export default {
    name: 'BaseSettlementOrgList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      BaseSettlementOrgModal,
      BaseLineConfigModal,
      BaseAuditModal,
      BaseSettlemenDetailModal
  },
    data () {
      return {
        description: '结算组织管理页面',
        isorter:{
          column: 'balance',
          order: 'asc',
        },
        queryParam: {
          status:'1'
        },
        allowClear:true,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'组织ID',
            align:"center",
            dataIndex: 'id'
          },
          {
            title:'组织名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'来源',
            align:"center",
            dataIndex: 'source_dictText'
          },
          {
            title:'账户余额',
            align:"center",
            dataIndex: 'balance',
            sorter: true,
          },
          {
            title:'实际余额',
            align:"center",
            dataIndex: 'realBalance',
            sorter: true,
          },
          {
            title:'7天平均费用',
            align:"center",
            dataIndex: 'weekAverageAmount'
          },
          {
            title:'临时额度',
            align:"center",
            dataIndex: 'temporaryBalance'
          },
          {
            title:'临时额度天数',
            align:"center",
            dataIndex: 'temporaryDayNum'
          },
          {
            title:'管理用户',
            align:"center",
            dataIndex: 'users',
            sorter: true,
          },
          // 暂时隐藏   0826
          {
             title:'认证情况',
             align:"center",
             dataIndex: 'authStatus_dictText',
             sorter: true,
           },
           {
             title:'凭证同步金蝶',
             align:"center",
             dataIndex: 'izSyncVoucher_dictText',
           },
          //  {
          //    title:'技术服务费',
          //    align:"center",
          //    dataIndex: 'technicalProption',
          //    sorter: true,
          //    customRender: function (t, r, index) {              
          //    return t<1?((t*100).toFixed(2))+'%':t+'%';
          //  },
          //  },
          //  {
          //    title:'包材费(一票多件)',
          //    align:"center",
          //    sorter: true,
          //    dataIndex: 'packageFee'
          //  },
          //  {
          //    title:'操作费',
          //    align:"center",
          //    sorter: true,
          //    dataIndex: 'operateFee'
          //  },
           {
             align:"center",
             dataIndex: 'ecStatus_dictText',
             slots: { title: 'ecStatusTitle' },
             customCell:(record, rowIndex)=>{		//在此处定义
               if(record.ecStatus_dictText === '正常'){
                 return {style:{'color': '#088f01'}}    //return 想要设置的样式
               }else {
                 return {style:{'color': '#ff0000'}}    //return 想要设置的样式
               }
             },
            
           },
           {
             title:'易仓账号状态',
            align:"center",
            dataIndex: 'ecUserStatus_dictText',
             customCell:(record, rowIndex)=>{		//在此处定义
               if(record.ecUserStatus_dictText === '正常'){
                 return {style:{'color': '#088f01'}}    //return 想要设置的样式
               }else {
                 return {style:{'color': '#ff0000'}}    //return 想要设置的样式
               }
             },
          },
           {
             title:'启用状态',
             align:"center",
             dataIndex: 'status_dictText',
           },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
            sorter: true,
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/finance/baseSettlementOrg/list",
          delete: "/finance/baseSettlementOrg/delete",
          deleteBatch: "/finance/baseSettlementOrg/deleteBatch",
          exportXlsUrl: "/finance/baseSettlementOrg/exportXls",
          importExcelUrl: "finance/baseSettlementOrg/importExcel",
          changeEcUserStatus: "/finance/logisticsSettlementEcUser/editEcUser",
          changeEcDeliverStatus: "/finance/baseSettlementOrg/syncEcangDeliverInfo",
          asyncAccountData:'/finance/baseSettlementOrg/asyncAccount'
        },
        dictOptions:{},
        superFieldList:[],
        ecUserChangeLoading: false,
        syncLoading:false,
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      selectRowData(record,index){
        const that = this
        return {
          on:{
            dblclick:() => {
              this.handleDetail(record)
            }
          }
        }
      },
      getPopupContainer(trigger) {
      return trigger.parentNode
    },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'组织名称',dictCode:''})
        fieldList.push({type:'string',value:'source_dictText',text:'来源',dictCode:''})
        fieldList.push({type:'string',value:'balance',text:'账户余额',dictCode:''})
        fieldList.push({type:'string',value:'realBalance',text:'实际余额',dictCode:''})
        fieldList.push({type:'string',value:'weekAverageAmount',text:'7天平均费用',dictCode:''})
        fieldList.push({type:'string',value:'temporaryBalance',text:'临时额度',dictCode:''})
        fieldList.push({type:'string',value:'temporaryDayNum',text:'临时额度天数',dictCode:''})
        fieldList.push({type:'string',value:'users',text:'管理用户',dictCode:''})
        fieldList.push({type:'string',value:'authStatus_dictText',text:'认证情况',dictCode:''})
        fieldList.push({type:'string',value:'ecStatus_dictText',text:'仓库发货状态',dictCode:''})
        fieldList.push({type:'string',value:'ecUserStatus_dictText',text:'易仓账号状态',dictCode:''})
        fieldList.push({type:'string',value:'createTime',text:'创建时间',dictCode:''})
        this.superFieldList = fieldList
      },
       //重置
      searchReset() {
        this.queryParam = {
          status:'1'
        }
        this.loadData(1);
      },
      enableBt(record){
        const that = this;
        const param = {accountId: record.id, status:'1'}
        that.ecUserChangeLoading = true
        postFormAction(that.url.changeEcDeliverStatus,param).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.ecUserChangeLoading=false;
        });
      },
      enableUserBt(record){
        const that = this;
        const param = {accountId: record.id, status:'1'}
        that.ecUserChangeLoading = true
        postFormAction(that.url.changeEcUserStatus,param).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.ecUserChangeLoading=false;
        });
      },
      disableBt(record){
        const that = this;
        const param = {accountId: record.id, status:'0'};
        that.ecUserChangeLoading = true
        postFormAction(that.url.changeEcDeliverStatus,param).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.ecUserChangeLoading=false;
        });
      },
      disableUserBt(record){
        const that = this;
        const param = {accountId: record.id, status:'0'};
        that.ecUserChangeLoading = true
        postFormAction(that.url.changeEcUserStatus,param).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.ecUserChangeLoading=false;
        });
      },
      //额度配置
      handleLineConfig(record){
        this.$refs.lineConfigModal.title='额度配置'
        this.$refs.lineConfigModal.edit(record)
        this.$refs.lineConfigModal.visible=true
      },
      //审核
      handleCheck(record){
        this.$refs.baseAuditModal.title='审核'
        this.$refs.baseAuditModal.disableSubmit = true;
        this.$refs.baseAuditModal.edit(record)
        this.$refs.baseAuditModal.visible=true
      },
      //详情
      handleDetails(record){
        this.$refs.detailModal.title='详情'
        this.$refs.detailModal.edit(record)
        this.$refs.detailModal.disableSubmit = true;
        this.$refs.detailModal.visible=true
      },     
      //同步结算组织数据
      handleSyncData(){
        const that = this;
        that.syncLoading=true
         postAction(that.url.asyncAccountData).then(res=>{             
          if (res.success) {
            that.$message.success(res.message);
          } else {
            that.$message.warning(res.message);
           }
           }).finally(() => {
           that.syncLoading=false
        });             
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  /deep/ .ant-tooltip {
  max-width: 700px !important;
}
</style>